package com.proactive.webapp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.proactive.webapp.dao.ActivityImageDAO;
import com.proactive.webapp.entity.ActivityImage;
import com.proactive.webapp.entity.CardTemplate;

public class ActivityImageDAOImpl implements ActivityImageDAO {
	private DataSource dataSource;
	private Connection conn = null;
	private ResultSet rs = null;
	private PreparedStatement smt = null;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public List<ActivityImage> getList(int activityID) {
		// TODO Auto-generated method stub
		List<ActivityImage> activityImageList = new ArrayList<ActivityImage>();
		String sql = "SELECT * FROM activity_image WHERE activityID=?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, activityID);
			rs = smt.executeQuery();
			while (rs.next()) {
				ActivityImage activityImage = new ActivityImage();
				activityImage.setId(rs.getInt("activityImageID"));
				activityImage.setImagePath(rs.getString("activityImage"));
				activityImageList.add(activityImage);
			}
			rs.close();
			smt.close();

		} catch (SQLException e) {
			throw new RuntimeException(e);

		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}
		}
		return activityImageList;
	}

	public void insert(ActivityImage activityImage) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO activity_image(activityID,activityImage) "
				+ "VALUES(?,?)";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, activityImage.getAid());
			smt.setString(2, activityImage.getImagePath());
			smt.executeUpdate();
			smt.close();

		} catch (SQLException e) {
			throw new RuntimeException(e);

		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}
		}
	}

	public void delete(ActivityImage activityImage) {
		String sql = " DELETE FROM activity_image WHERE activityImageID=?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, activityImage.getId());
			smt.executeUpdate();
			smt.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}
		}
	}

	@Override
	public ActivityImage get(ActivityImage activityImage) {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM activity_image WHERE activityImageID=?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, activityImage.getId());
			rs = smt.executeQuery();
			while (rs.next()) {
				activityImage.setAid(rs.getInt("activityID"));
				activityImage.setImagePath(rs.getString("activityImage"));
			}
			rs.close();
			smt.close();

		} catch (SQLException e) {
			throw new RuntimeException(e);

		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}
		}
		return activityImage;

	}
}
